import { listTree, save, remove, update } from '@/services/system/duty';
import { treeDataWrapper } from '@/utils/common';
import { message } from 'antd';
import { authDuty, listDutyAcl } from '@/services/system/acl';

const Model = {
  namespace: 'SystemDutyModel',
  state: {
    resourceIdList: [],
    authVisible: false,
    formVisible: false,
    formType: 'raise',
    treeData: [],
    selectData: null,
    duty: {
      parentId: null,
      code: '',
      name: '',
      sources: 0
    },
  },
  effects: {
    *listTreeEffect(_, { call, put }) {
      const response = yield call(listTree);
      const listData = Array.isArray(response.data) ? response.data : [];

      yield put({ type: 'selectDataReducer', payload: listData.length > 0 ? listData[0] : null });

      treeDataWrapper(listData);
      yield put({ type: 'ListTreeReducer', payload: listData });
    },
    *saveEffect({ payload }, { call }) {
      const response = yield call(save, payload);

      if (response.success) message.success('新建职务成功!');
      else message.error('新建职务失败!');
    },
    *updateEffect({ payload }, { call }) {
      const response = yield call(update, payload);

      if (response.success) message.success('修改职务成功!');
      else message.error('修改职务失败!');
    },
    *removeEffect(_, { select, call }) {
      const { selectData } = yield select((state) => state.SystemDutyModel);
      const response = yield call(remove, selectData.id);

      if (response.success) message.success('删除职务成功!');
      else message.error('删除职务失败!');
    },
    *listAclEffect(_, { select, call, put }) {
      const { selectData } = yield select((state) => state.SystemDutyModel);
      const response = yield call(listDutyAcl, selectData.id);
      const listData = Array.isArray(response.data) ? response.data : [];

      yield put({ type: 'resourceIdListReducer', payload: listData });
    },
    *authEffect(_, { select, call }) {
      const { selectData, resourceIdList } = yield select((state) => state.SystemDutyModel);
      const response = yield call(authDuty, {principalId: selectData.id, resourceIdList});

      if (response.success) message.success('职务授权成功!');
      else message.error('职务授权失败!');
    },
  },
  reducers: {
    ListTreeReducer(state, { payload }) {
      return { ...state, treeData: payload };
    },
    selectDataReducer(state, { payload }) {
      return { ...state, selectData: payload };
    },
    changeFormVisibleReducer(state, { payload }) {
      return { ...state, formVisible: payload };
    },
    changeAuthVisibleReducer(state, { payload }) {
      return { ...state, authVisible: payload };
    },
    formHandleReducer(state, { payload }) {
      if (payload === 'raise') {
        return { 
          ...state, 
          formVisible: true,  
          formType: payload, 
          duty: {
            parentId: null,
            code: '',
            name: '',
            sources: 0
          }
        };
      }

      if (payload === 'modify') {
        return { ...state, formVisible: true,  formType: payload, duty: state.selectData};
      }
    },
    resourceIdListReducer(state, { payload }) {
      return { ...state, resourceIdList: payload };
    }
  }
};

export default Model;
